<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
* News 管理 
*/
class News extends CI_Controller {
	
	private $_data = array();
	public $type=array(
		
	);
	public function __construct(){
		parent::__construct();
		$this->load->helper('url');
		
		//$this->load->model('product_model');
		//$this->type=$this->product_model->get_producttype();
	}

	public function display($id = 0){
		$sql = "SELECT a.*, b.title AS typetitle FROM {$this->db->dbprefix}article a
		LEFT JOIN {$this->db->dbprefix}articletype b ON a.typeid = b.id
		WHERE a.id={$id}";
		$q = $this->db->query($sql);
		$a = $q->first_row();
		if($a){
			$this->_data['title'] = $a->title;
			$this->db->where('id',$a->id);
			$this->db->update('article', array('views'=>intval($a->views)+1,'id'=>$a->id));
		}else{
			show_404();
		}
		$this->_data['news'] = $a;

		$this->load->view('news/display', $this->_data);
	}
	/**
	* 文章首页
	*/
	public function index(){
		$this->_data['title'] = '文章列表';
		$sql = "SELECT COUNT(*) as cnt FROM {$this->db->dbprefix}article";
		$q = $this->db->query($sql);
		$a = $q->result();
		$b = $a[0];
		$per_page = 10;
		$total_rows = $b->cnt;
		$cur_page = $this->uri->segment(3);
		if(!$cur_page) $cur_page = 0;
		//分页
		$this->load->library('pagination');
		$config = array(
			'base_url'		=> site_url('news/index'),
			'total_rows'	=> $total_rows,
			'per_page'		=> $per_page,
			//'uri_segment'	=> 3
			);
		
		$this->pagination->initialize($config); 
		
		$this->_data['pagination'] = $this->pagination->create_links();
		
		$sql = "SELECT a.*, b.title AS typetitle FROM {$this->db->dbprefix}article a
		LEFT JOIN {$this->db->dbprefix}articletype b ON a.typeid = b.id
		ORDER BY a.id desc 
		LIMIT {$cur_page} , {$per_page}
		";
		$q = $this->db->query($sql);
		$a = $q->result();
		$this->_data['values'] = $a ;

		$this->load->view('news/index', $this->_data);
	}

	public function myclass($typeid, $cur_page = 0){
		$sql = "SELECT COUNT(*) as cnt FROM {$this->db->dbprefix}article where typeid=".$typeid;
		$q = $this->db->query($sql);
		$a = $q->result();
		$b = $a[0];
		$per_page = 10;
		$total_rows = $b->cnt;

		if($total_rows == 0){
			show_404();
		}

		$type = $this->db->get_where('articletype', array('id'=>$typeid),1 );
		$a = $type->result();
		$b = $a[0];
		$this->_data['type'] = $b;

		//分页
		$this->load->library('pagination');
		$config = array(
			'base_url'		=> site_url('news/myclass/'.$typeid),
			'total_rows'	=> $total_rows,
			'per_page'		=> $per_page,
			'uri_segment'	=> 4
			);
		
		$this->pagination->initialize($config); 
		
		$this->_data['pagination'] = $this->pagination->create_links();
		
		$sql = "SELECT a.*, b.title AS typetitle FROM {$this->db->dbprefix}article a
		LEFT JOIN {$this->db->dbprefix}articletype b ON a.typeid = b.id
		WHERE b.id={$typeid}
		ORDER BY a.id desc 
		LIMIT {$cur_page} , {$per_page}
		";
		$q = $this->db->query($sql);
		$a = $q->result();
		$this->_data['values'] = $a ;

		$this->load->view('news/myclass', $this->_data);
	}

}

/* End of file news.php */
/* Location: ./application/controllers/news.php */